/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace TeachMe.Application
{
    public enum WindowEdge
    {
        TopLeft,
        TopRight,
        BottomLeft,
        BottomRight
    }

    public class WindowLocations
    {
        private readonly Dictionary<WindowEdge, int> _data = new Dictionary<WindowEdge, int>();
        private readonly int _screenLeft;
        private readonly int _screenTop;

        public WindowLocations()
        {
            _screenLeft = 0;
            _screenTop = 0;
        }

        public WindowLocations(int screenLeft, int screenTop)
        {
            // because form size is limited to screen size i must add absoulte offsets if any offset is lesser than zero

            _screenTop = screenTop;
            _screenLeft = screenLeft;
        }

        public void Populate(int left, int top, Size size)
        {
            _data.Clear();

            if (_screenTop < 0)
            {
                top += Math.Abs(_screenTop);
            }
            if (_screenLeft < 0)
            {
                left += Math.Abs(_screenLeft);
            }

            _data.Add(WindowEdge.TopLeft, GetScreen(new Point(left, top)));
            _data.Add(WindowEdge.TopRight, GetScreen(new Point(left + size.Width, top)));
            _data.Add(WindowEdge.BottomLeft, GetScreen(new Point(left, top + size.Height)));
            _data.Add(WindowEdge.BottomRight, GetScreen(new Point(left + size.Width, top + size.Height)));
        }

        public bool IsVisible(WindowEdge edge)
        {
            return _data[edge] != -1;
        }

        public bool IsFullyVisible()
        {
            return IsVisible(WindowEdge.TopLeft) && IsVisible(WindowEdge.TopRight) && IsVisible(WindowEdge.BottomLeft) && IsVisible(WindowEdge.BottomRight);
        }

        public bool IsPartiallyVisible()
        {
            return IsVisible(WindowEdge.TopLeft) || IsVisible(WindowEdge.TopRight) || IsVisible(WindowEdge.BottomLeft) || IsVisible(WindowEdge.BottomRight);
        }

        private static int GetScreen(Point location)
        {
            int result = 0;
            foreach (Screen screen in Screen.AllScreens)
            {
                if (screen.WorkingArea.Contains(location))
                {
                    return result;
                }
                result++;
            }
            return -1;
        }
    }
}
